home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / libs / ttengine.lha / ttengine-4.1 / Examples / TextLength / txlen.c < prev   
C/C++ Source or Header  |  2002-09-29  |  6KB  |  163 lines

  1. /* test ttrender */
  2.  
  3. #define __NOLIBBASE__
  4.  
  5. #include <proto/dos.h>
  6. #include <proto/exec.h>
  7. #include <proto/intuition.h>
  8. #include <proto/graphics.h>
  9. #include <proto/ttengine.h>
  10. #include <proto/asl.h>
  11.  
  12. #include <libraries/ttengine.h>
  13.  
  14. extern struct Library *SysBase, *DOSBase;
  15.  
  16. struct Library *TTEngineBase, *IntuitionBase, *GfxBase, *AslBase;
  17.  
  18. /*----------------------------------------------------------------------------------------------------*/
  19.  
  20. static STRPTR get_font_name(struct Library *AslBase)
  21.   {
  22.     struct FileRequester *freq;
  23.     STRPTR name = NULL;
  24.  
  25.     if (freq = AllocAslRequestTags(ASL_FileRequest, TAG_END))
  26.       {
  27.         if (AslRequestTags(freq,
  28.           ASLFR_TitleText, (ULONG)"Select TrueType font",
  29.           ASLFR_InitialDrawer, (ULONG)"FONTS:",
  30.           ASLFR_DoPatterns, TRUE,
  31.           ASLFR_InitialPattern, (ULONG)"#?.ttf",
  32.           ASLFR_RejectIcons, TRUE,
  33.           TAG_END))
  34.           {
  35.             ULONG namelen = strlen(freq->fr_File) + strlen(freq->fr_Drawer) + 4;
  36.  
  37.             if (name = AllocVec(namelen + 1, MEMF_ANY | MEMF_CLEAR))
  38.               {
  39.                 strncpy(name, freq->fr_Drawer, namelen);
  40.                 AddPart(name, freq->fr_File, namelen);
  41.               }
  42.           }
  43.         FreeAslRequest(freq);
  44.       }
  45.     return name;
  46.   }
  47.  
  48. /*----------------------------------------------------------------------------------------------------*/
  49.  
  50. static VOID free_font_name(STRPTR name)
  51.   {
  52.     if (name) FreeVec(name);
  53.   }
  54.  
  55. /*----------------------------------------------------------------------------------------------------*/
  56.  
  57. void measured_text(STRPTR text, UWORD y, struct RastPort *rp)
  58.   {
  59.     struct TextExtent te;
  60.     UWORD len;
  61.     ULONG pixlen;
  62.  
  63.     len = strlen(text);
  64.     pixlen = TT_TextLength(rp, text, len);
  65.  
  66.     SetAPen(rp, 2);
  67.     Move(rp, 10, y);
  68.     Draw(rp, 10, y + 7);
  69.     Move(rp, 10, y + 5);
  70.     Draw(rp, 10 + pixlen, y + 5);
  71.     Move(rp, 10 + pixlen, y);
  72.     Draw(rp, 10 + pixlen, y + 7);
  73.  
  74.     SetAPen(rp, 1);
  75.     Move(rp, 10, y);
  76.     TT_Text(rp, text, len);
  77.   }
  78.  
  79.  
  80. int Main (void)
  81.   {
  82.     struct Window *win;
  83.     STRPTR fontname;
  84.  
  85.     if (GfxBase = OpenLibrary("graphics.library", 39))
  86.       {
  87.         if (IntuitionBase = OpenLibrary("intuition.library", 39))
  88.           {
  89.             if (AslBase = OpenLibrary("asl.library", 38))
  90.               {
  91.                 if (fontname = get_font_name(AslBase))
  92.                   {
  93.                     if (TTEngineBase = OpenLibrary("ttengine.library", 0))
  94.                       {
  95.                         if (win = OpenWindowTags(NULL,
  96.                           WA_Top, 25,
  97.                           WA_Left, 0,
  98.                           WA_Width, 640,
  99.                           WA_Height, 210,
  100.                           WA_CloseGadget, TRUE,
  101.                           WA_DragBar, TRUE,
  102.                           WA_DepthGadget, TRUE,
  103.                           WA_IDCMP, IDCMP_CLOSEWINDOW,
  104.                           WA_Title, (ULONG)"TT_TextLength() test",
  105.                           TAG_END))
  106.                           {
  107.                             ULONG sigmask, signals;
  108.                             BOOL running = TRUE;
  109.                             struct RastPort *rp = win->RPort;
  110.                             APTR font;
  111.  
  112.                             if (font = TT_OpenFont(
  113.                               TT_FontFile, (ULONG)fontname,
  114.                               TT_FontSize, 18,
  115.                             TAG_END))
  116.                               {
  117.                                 TT_SetFont(rp, font);
  118.                                 TT_SetAttrs(rp,
  119.                                   TT_Window, (ULONG)win,
  120.                                 TAG_END);
  121.  
  122.                                 SetDrMd(rp, JAM1);
  123.  
  124.                                 measured_text("TT_TextLength() example", 45, rp);
  125.                                 measured_text("White lines shows text length in pixels as computed by the function.", 70, rp);
  126.                                 measured_text("Note that TT_TextLength() returns only \"pen advance\".", 95, rp);
  127.                                 measured_text("Use TT_TextExtent() if you want to get precise bounding box.", 120, rp);
  128.  
  129.                                 TT_CloseFont(font);
  130.                               }
  131.                             else PutStr("Font open failed.\n");
  132.  
  133.                             sigmask = SIGBREAKF_CTRL_C | (1 << win->UserPort->mp_SigBit);
  134.                             while (running)
  135.                               {
  136.                                 signals = Wait(sigmask);
  137.                                 if (signals & SIGBREAKF_CTRL_C) running = FALSE;
  138.                                 if (signals & (1 << win->UserPort->mp_SigBit))
  139.                                   {
  140.                                     struct IntuiMessage *imsg;
  141.  
  142.                                     while (imsg = (struct IntuiMessage*)GetMsg(win->UserPort))
  143.                                       {
  144.                                         if (imsg->Class == IDCMP_CLOSEWINDOW) running = FALSE;
  145.                                         ReplyMsg((struct Message*)imsg);
  146.                                       }
  147.                                   }
  148.                               }
  149.                             CloseWindow(win);
  150.                           }
  151.                         CloseLibrary(TTEngineBase);
  152.                       }
  153.                     free_font_name(fontname);
  154.                   }
  155.                 CloseLibrary(AslBase);
  156.               }
  157.             CloseLibrary(IntuitionBase);
  158.           }
  159.         CloseLibrary(GfxBase);
  160.       }
  161.     return 0;
  162.   }
  163.